﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using COM.DoorsWindows.Model;
using Webdiyer.WebControls.Mvc;

namespace COM.DoorsWindows.Web.Areas.Admin.Controllers
{
    public class TypeClassController : Controller
    {
        private DoorsAndWindowsDBEntities db = new DoorsAndWindowsDBEntities();

        //
        // GET: /Admin/TypeClass/

        public ViewResult Index(int? page = 1)
        {

            var totalCount = db.TypeList.Count(o => o.ParentID != 0);
            var pageIndex = page ?? 1;
            var userList = db.TypeList.Where(o => o.ParentID != 0).OrderBy(o => o.Id).Skip((pageIndex - 1) * 2).Take(2).ToList();
            PagedList<TypeList> mPage = userList.AsQueryable().ToPagedList(0, 2);
            mPage.TotalItemCount = totalCount;
            mPage.CurrentPageIndex = (int)(page ?? 1);
            ViewBag.ParentID = new SelectList(db.TypeList.Where(o => o.ParentID == 0), "ParentID", "Name");
            return View(mPage);
        }

        //
        // GET: /Admin/TypeClass/Details/5

        public ViewResult Details(int id)
        {
            TypeList typelist = db.TypeList.Find(id);
            return View(typelist);
        }

        //
        // GET: /Admin/TypeClass/Create

        public ActionResult Create()
        {
            ViewBag.ParentID = new SelectList(db.TypeList.Where(o => o.ParentID == 0), "Id", "Name");

            return View();
        }

        //
        // POST: /Admin/TypeClass/Create

        [HttpPost]
        public ActionResult Create(TypeList typelist)
        {
            if (ModelState.IsValid)
            {
                typelist.CreateTime = DateTime.Now;
                db.TypeList.Add(typelist);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(typelist);
        }

        //
        // GET: /Admin/TypeClass/Edit/5

        public ActionResult Edit(int id)
        {
            TypeList typelist = db.TypeList.Find(id);
            ViewBag.ParentID = new SelectList(db.TypeList.Where(o => o.ParentID == 0), "Id", "Name", typelist.ParentID);
            return View(typelist);
        }

        //
        // POST: /Admin/TypeClass/Edit/5

        [HttpPost]
        public ActionResult Edit(TypeList typelist)
        {
            if (ModelState.IsValid)
            {
                db.Entry(typelist).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(typelist);
        }

        //
        // GET: /Admin/TypeClass/Delete/5

        public ActionResult Delete(int id)
        {
            TypeList typelist = db.TypeList.Find(id);
            return View(typelist);
        }

        //
        // POST: /Admin/TypeClass/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TypeList typelist = db.TypeList.Find(id);
            db.TypeList.Remove(typelist);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}